<%@ page language="java" import="java.sql.*,javax.naming.*,javax.sql.*, java.math.BigDecimal" pageEncoding="utf-8"%>

<html>
<head>

<link type="text/css" href="../css/global.css" rel="stylesheet" />
<link type="text/css" href="../css/simpletable.css" rel="stylesheet" />		

<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
	$(document).ready(
			function() {
				//alert($("#finised").val());
				if($("#DROPID").val()==""){
					$("#submit").hide();
				}
				else if ($("#finised").val() == 'undefined' ){
					$("#submit").hide();
				}
				else{
					$("#submit").show();
				}
				$("#query").click(
						function() {

							/******************
							alert('Handler for .change() called.');
							
							/******************/
							$.ajaxSetup({ cache: false });
							//$.getJSON("qservice.jsp", { dropid: $("#DROPID").val() }, function(data, state) {
							$.getJSON("qservice.jsp", { dropid: $("#DROPID").val() }, function(data, state) {

								/*****************debug/
								alert(data.jsonChildarray.length);
								/******************/
								$("#submit").show();								
								if(data.Status>= '2' && data.jsonChildarray.length==0){
									alert("已完成複盤確認");
									$("#submit").hide();
								}
								//else if(data.jsonChildarray.length==0 && data.Status == '-1'){
								else if(data.Status === '-1'){
									alert("查無此複盤流水號");
									$("#submit").hide();
								}
								
								$("#mytable").find("tr:gt(0)").remove();
								$.each(data.jsonChildarray, function(i, data) {
									//alert(data.CountQty2);
									var UPCCode, Lottable05, Qty;
									if(data.UPCCode=="null"){
										UPCCode = "&nbsp;";
									}
									else{
										UPCCode = data.UPCCode;
									}
									if(typeof data.Lottable05 == 'undefined'){
										Lottable05 = "&nbsp;";
									}
									else{
										Lottable05 = data.Lottable05;
									}
									if(data.Qty==""){
										Qty = "&nbsp;";
									}
									else{
										Qty = data.Qty;
									}
									
									if(data.CountQty2!=0){
									
									$("#mytable").append(
											'<tr> <td>' + data.Seq2 + '</td><td name="Loc" >' 
													+ data.Loc + '</td><td name="Sku" >'
													+ data.Sku + '</td><td name="Descr" >'
													+ data.Descr + '</td><td name="UPCCode" >'
													+ UPCCode + '</td><td name="Lottable05" >'
													+ Lottable05 + '</td><td name="Casecnt" align="right">'
													+ data.Casecnt + '</td><td name="Qty" align="right">'
													+ Qty + '</td> <td><input type="text" style="width:100px;" STYLE="text-align:right" name="CountQty2" maxlength="19" value ='
													+ data.CountQty2
													+ ' /> </td>'
													/*
													+ '<input type="hidden" name="Loc" value="' 
													+ data.Loc + '">'
													+ '<input type="hidden" name="Sku" value="' 
													+ data.Sku + '">'
													*/
													+ '<input type="hidden" name="Seq2" value="' 
													+ data.Seq2 + '">'
													+ '</tr>');
									}
									else{
										$("#mytable").append(
											'<tr> <td>' + data.Seq2 + '</td><td name="Loc" >' 
													+ data.Loc + '</td><td name="Sku" >'
													+ data.Sku + '</td><td name="Descr" >'
													+ data.Descr + '</td><td name="UPCCode" >'
													+ UPCCode + '</td><td name="Lottable05" >'
													+ Lottable05 + '</td><td name="Casecnt" align="right">'
													+ data.Casecnt + '</td><td name="Qty" align="right">'
													+ Qty + '</td> <td><input type="text" style="width:100px;" STYLE="text-align:right" name="CountQty2" maxlength="19" /></td>'
													/*
													+ '<input type="hidden" name="Loc" value="' 
													+ data.Loc + '">'
													+ '<input type="hidden" name="Sku" value="' 
													+ data.Sku + '">'
													*/
													+ '<input type="hidden" name="Seq2" value="' 
													+ data.Seq2 + '">'
													+ '</tr>');
									
									}
								});
								/******************/
								//if ( k == -1 ){}
								
							});
							/******************/
						});

			
			});
	
	
	$("input[name='CountQty2']").live("keyup", function(e) {
		/* ENTER PRESSED*/
		//alert("keyup");
		//alert($(this).val());
		
		var value = $(this).val();
		if((/^(\+|-)?\d+$/.test( value ))|| value<0){
			return true;
		}else{
			alert("数量中请输入正整数！");
			$(this).val("");
			return false;
		}
		
		if (e.keyCode == 13) {
			/* FOCUS ELEMENT */
			var inputs = $(this).parents("form").eq(0).find(":input");
			var idx = inputs.index(this);

			if (idx == inputs.length - 1) {
				inputs[0].select();
			} else {
				inputs[idx + 1].focus(); //  handles submit buttons
				inputs[idx + 1].select();
			}
			return false;
		}
	});

	function processJson(data) {
		// 'data' is the json object returned from the server 
		alert(data.message);
	}

	function addrow() {
		//var form_url = $("#myform").attr("action");
		//alert("Before - action=" + form_url);       

		$("#myform").attr("action", "addrow.jsp");
		//alert("After - action = "+$("#myform").attr("action"));
		//submit the form
		$("#myform").submit();
	}

	function getURLParameter(name) {
		return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(
				Location.search) || [ , null ])[1]);
	}
</script>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">


<body>
	<%
			String dropid = request.getParameter("DROPID");
	%>
	<form id='myform' action="updatetable.jsp" method="post" ALIGN="CENTER" >
		<table class="gridBody" ALIGN="CENTER"><tr>
			<th Width="100">複盤流水號</th>
			<th Width="100"><input id="DROPID" type="text" name="DROPID" value = '<%=dropid!=null?dropid:"" %>'/></th>
			<th Width="100" COLSPAN="8"><button id="query">查詢</button></th>
		</tr>
		</table>
		<table class="gridBody" id="mytable"  border =1 ALIGN="CENTER">
			<tr>
				<th Width="50">序號</th>
				<th Width="100">儲位</th>
				<th Width="100">商品代碼</th>
				<th Width="100">品名</th>
				<th Width="100">UPC</th>
				<th Width="100">效期</th>
				<th Width="100">箱入數</th>
				<th Width="100">庫存數</th>
				<th Width="100">複盤數量</th>
			</tr>
			
			<%
			
			if(dropid !=null && dropid != ""){
				boolean finished = true;
				Connection conn = null;
				PreparedStatement pstmt = null;
				String sql = "select Status, CCDROPID2, Seq2, Loc, Sku, Descr, UPCCode, Lottable05, min(Casecnt) as Casecnt, DisplaySystemCount, sum(Qty) as Qty, CountQty2 from CCDET WHERE CCDROPID2 = ? and Status = '3' "
		  			+ "group by Status, CCKEY, CCDROPID2, Seq2, Loc, Sku, Descr, UPCCode, Lottable05, DisplaySystemCount, CountQty2 ";
		    	try {
					Class.forName("oracle.jdbc.driver.OracleDriver");
			
					Context ctx = new InitialContext();
					DataSource ds = (DataSource) ctx.lookup("java:jdbc/WMPRD_WMWHSE1");
					conn = ds.getConnection();
			
					
			    	pstmt = conn.prepareStatement(sql);
					pstmt.setString(1 , dropid);
					ResultSet rs = pstmt.executeQuery();
					while (rs.next()){
						finished = false;
						int Seq2 = rs.getInt("Seq2") ;
						%>
						<tr>
						<td><%=Seq2 %></td>
						<td><%=rs.getString("Loc") %></td>
						<td><%=rs.getString("Sku") %></td>
						<td><%=rs.getString("Descr") %></td>
						<td><%=rs.getString("UPCCode").equalsIgnoreCase("null")?"&nbsp;":rs.getString("UPCCode")%></td>
						<td><%=rs.getString("Lottable05")==null?"&nbsp;":rs.getString("Lottable05")%></td>
						<td><%=rs.getString("Casecnt") %></td>
						<td><%=rs.getInt("DisplaySystemCount")==1?rs.getString("Qty"):"&nbsp;"%></td>
						<td><input type="text" style="width:100px;" STYLE="text-align:right" name="CountQty2" maxlength="19"  value="<%=rs.getInt("CountQty2")==0?"":rs.getInt("CountQty2")%>"/></td>
						<td><input type="hidden" name="Seq2" value="<%=Seq2 %>"></td>			
						</tr>
							<%
						
							
						}
						conn.close();
					} catch (SQLException sqle) {
						//out.println("SQL Exception : " + sqle);
						System.out.println("SQL Exception : " + sqle);
					}
					if(finished){
						%>
						<tr>
						<td colspan="9" align="center"><div id="finised" >此流水號已完成複盤確認</div></td>
						</tr>
					
						<%
					}
					}
						%>
					
			
		</table>
		<table class="gridBody" ALIGN="CENTER">
		<tr>
		
		<th Width="100"><div id="submit" ><input type="submit" class="wmsButton" value="確認" /></div></th>
		</tr>
		</table>

	</form>
</body>
</html>

